<template>
    <div class="PhasedPlanItem">
        <el-dialog
            v-drag
            :title="isTitleValue"
            :visible.sync="showDialogFroms"
            width="900px"
            :append-to-body="true"
            top="20vh"
            class="PhasedPlanItem"
            :close-on-click-modal="false"
            center
        >
            <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="155px">
                <el-row>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="分期计划编号:" class="text_fl" prop="itemNo">
                                <el-input
                                    :disabled="(isTitleValue=='修改分期计划')"
                                    v-model.trim="formData.itemNo"
                                ></el-input>
                            </el-form-item>
                        </div>
                    </el-col>

                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="分期名称:" class="text_fl" prop="itemCm">
                                <el-input v-model.trim="formData.itemCm"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="费率:" class="text_fl" prop="appFeeratio">
                                <el-input v-model.trim="formData.appFeeratio"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="分期期数:" class="text_fl" prop="nbrMths">
                                <el-input v-model.trim="formData.nbrMths"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="手续费分摊方式:" class="text_fl" prop="paytype">
                                <el-input v-model.trim="formData.paytype"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="本金分摊方式:" class="text_fl" prop="paytype">
                                <el-input v-model.trim="formData.priType"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="折扣率:" class="text_fl">
                                <el-input v-model.trim="formData.discountRate"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="text_center width30">
                            <el-form-item label="利率:" class="text_fl">
                                <el-input type="textarea" v-model.trim="formData.interestRate"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-circle-close"
                    @click="formClose"
                >取 消</el-button>
                <el-button
                    type="primary"
                    v-if="(isTitleValue == '修改分期计划')"
                    size="small"
                    @click="updataInfo('ruleForm')"
                >修 改</el-button>
                <el-button
                    type="primary"
                    v-else
                    size="small"
                    @click="addInfo('ruleForm')"
                    icon="el-icon-circle-check"
                >确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { closePage } from '@/utils/Common';
import { isEmptyObject } from '@/utils/Object';

export default {
    name: 'PhasedPlanItem',
    props: {
        updataInfocurres: {
            type: Object,
            default: {},
        },
        // showDialogFrom: {
        //     type: Boolean,
        //     default: false,
        // },
        isTitleValue: {
            type: String,
            default: '新增',
        }

    },
    data() {
        return {
            formData: {
                id: '',
                itemNo: '',
                itemCm: '',
                appFeeratio: '',
                nbrMths: '',
                paytype: '',
                priType: '',
                interestRate: '',
                product: '0',
                currency: '156',
                discountRate: '',
            },
            showDialogFroms: true,
            rules: {
                itemNo: [{ required: true, message: '分期计划编号不能为空', trigger: 'blur' },],
                itemCm: [{ required: true, message: '分期名称不能为空', trigger: 'blur' },],
                appFeeratio: [{ required: true, message: '费率不能为空', trigger: 'blur' },],
                nbrMths: [{ required: true, message: '分期期数不能为空', trigger: 'blur' },],
                paytype: [{ required: true, message: '手续费分摊方式不能为空', trigger: 'blur' },],
            }
        }
    },
    mounted() {

        this.getUpdataInfoCur();
    },
    methods: {
        getUpdataInfoCur() {
            console.log('this.updataInfocurres', this.updataInfocurres);
            if (this.isTitleValue == '修改分期计划') {
                if (!isEmptyObject(this.updataInfocurres)) {
                    this.formData = Object.assign({}, this.updataInfocurres);
                }
            }

        },
        // 修改数据
        updataInfo() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = {
                        id: this.formData.id,
                        itemNo: this.formData.itemNo,
                        itemCm: this.formData.itemCm,
                        appFeeratio: this.formData.appFeeratio,
                        nbrMths: this.formData.nbrMths,
                        paytype: this.formData.paytype,
                        priType: this.formData.priType,
                        interestRate: this.formData.interestRate,
                        product: this.formData.product,
                        currency: this.formData.currency,
                        discountRate: this.formData.discountRate,
                    };
                    console.log('params', params);
                    // params = Object.assign({}, params, this.formData);
                    this.$post.MpprmPlan.updateIccMpprm(params).then((res) => {
                        console.log('增加分期计划', res);
                        if (res.data.success) {
                            this.$message.success('修改成功！');
                            this.formClose();
                        } else {
                            if (res.data.msg == '数据已存在') {
                                this.$message.error('分期计划编号已存在,数据添加失败！')
                            } else {
                                this.$message.error(res.data.msg + '!');
                            }

                        }
                    });
                }
            });
        },
        // 添加新的数据
        addInfo() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = {
                        itemNo: this.formData.itemNo,
                        itemCm: this.formData.itemCm,
                        appFeeratio: this.formData.appFeeratio,
                        nbrMths: this.formData.nbrMths,
                        paytype: this.formData.paytype,
                        priType: this.formData.priType,
                        interestRate: this.formData.interestRate,
                        product: this.formData.product,
                        currency: this.formData.currency,
                        discountRate: this.formData.discountRate,
                    };

                    // params = Object.assign({}, params, this.formData);
                    this.$post.MpprmPlan.addIccMpprm(params).then((res) => {
                        console.log('增加分期计划', res);
                        if (res.data.success) {
                            this.$message.success('添加成功！');
                            this.formClose();
                        } else {
                            if (res.data.msg == '数据已存在') {
                                this.$message.error('分期计划编号已存在,数据添加失败！')
                            } else {
                                this.$message.error(res.data.msg + '!');
                            }

                        }
                    });
                }
            });

        },
        // 取消
        formClose() {
            this.showDialogFroms = false;
            this.$emit('parnetFun');
        }
    },
}
</script>
<style  lang='scss'>
.PhasedPlanItem {
    .el-form-item__content {
        text-align: left;
    }
    .el-textarea__inner {
        height: 90px;
    }
}
</style>